package com.tongji.housediy.service
{
	import com.tongji.housediy.events.UserLoadandRegisterEvent.GetUserLoadEvent;
	import com.tongji.housediy.events.UserLoadandRegisterEvent.LoginEvent;
	import com.tongji.housediy.events.UserLoadandRegisterEvent.UserRegisterEvent;
	import com.tongji.housediy.events.designEvent.GetDesignsByUserEvent;
	import com.tongji.housediy.events.houseEvent.OpenHouseEvent;
	import com.tongji.housediy.model.UserInforModel;
	import com.tongji.housediy.view.titlewindows.OpenDesignTitleWindow;
	import com.tongji.housediy.vo.ShareSoap;
	
	import flash.system.Security;
	
	import mx.controls.Alert;
	import mx.managers.PopUpManager;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.soap.SOAPHeader;
	import mx.rpc.soap.WebService;
	
	import org.robotlegs.mvcs.Actor;

	/**
	 * 
	 * @author songjia
	 * 
	 */

	public class UserLoadandRegisterService extends Actor
	{						
		public var ws:WebService;
		private static const wsdl:String = "http://share.rryjw.com/ShareWebService.asmx?wsdl";
		private var userName:String;
		private var password:String;
		private var Email:String;
		private var IP:String;
		public var view:OpenDesignTitleWindow;
		[Inject]
		public var userModel:UserInforModel;
		
		public function UserLoadandRegisterService()
		{
			super();
			ws = new WebService();
			ws.wsdl = wsdl;
			ws.useProxy = false;
			
			ws.clearHeaders();
			var shareSoap:ShareSoap = new ShareSoap("tjdx", "tjdx");

			ws.addHeader(shareSoap);
			ws.loadWSDL();
			Security.loadPolicyFile("http://design.rryjw.com/crossdomain.xml");
			password = null;
			Email = null;
			IP = null;
			userName = null;
				
		}
		
		public function userLogin(userName:String, password:String):void
		{
			ws.UserLogin(userName, password);
			ws.UserLogin.addEventListener(ResultEvent.RESULT, loginResultHandler);
			ws.UserLogin.addEventListener(FaultEvent.FAULT, loginFaultHandler);
		}
		
		//登陆处理函数
		//
		//
		public function loginResultHandler(event:ResultEvent):void
		{
			if(event.result.toString() != "0")
			{
				userModel.isLogined = true;
				dispatch(new LoginEvent(LoginEvent.UPDATE_USER, '', '', event.result.toString()));
				dispatch(new LoginEvent(LoginEvent.LOGIN_SUCCESS, '', '', event.result.toString()));
				dispatch(new GetDesignsByUserEvent(GetDesignsByUserEvent.GET_DESIGNS_BY_USERID, event.result.toString()));
			}
			else
			{
				//不存在该用户
				Alert.show("不存在该登陆用户");
			}			
		}
		
		public function loginFaultHandler(event:FaultEvent):void
		{
			//登陆失败
			Alert.show(event.fault.toString());
		}
		
		//注册处理函数		
		public function Register(event:UserRegisterEvent):void
		{
			this.Email = event.Email;
			this.IP = event.IP;
			this.password = event.password;
			this.userName = event.userName;
			ws = new WebService();
			ws.wsdl = wsdl;
			ws.useProxy = false;
			
			ws.clearHeaders();
			var shareSoap:ShareSoap = new ShareSoap("tjdx", "tjdx");
			
			ws.addHeader(shareSoap);
			ws.loadWSDL();
			Security.loadPolicyFile("http://design.rryjw.com/crossdomain.xml");
			ws.CreateUser(event.userName, event.password, event.Email, this.IP);
			ws.CreateUser.addEventListener(ResultEvent.RESULT, RegisterHandler);
			ws.CreateUser.addEventListener(FaultEvent.FAULT, RegisterFaultHandler);
		}
		
		public function RegisterHandler(event:ResultEvent):void
		{			
			if(event.result.toString() != "-1")
			{
				var dispevent:GetUserLoadEvent = new GetUserLoadEvent(GetUserLoadEvent.GET_USER_LOAD, event.result.toString(), this.userName);
				dispatch(dispevent);
			}
			else
			{
				//该用户已被注册
				Alert.show("该用户已被注册");
			}	
		}
		
		public function RegisterFaultHandler(event:FaultEvent):void
		{
			//注册失败
			Alert.show("注册失败");
		}
	}
}